package com.hupu.jrs.ui.setting

import android.app.Activity
import android.app.Dialog
import android.app.DialogFragment
import android.app.Fragment
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.BaseAdapter
import android.widget.GridView
import android.widget.ImageView
import com.hupu.jrs.R
import com.hupu.jrs.injector.HasComponent
import com.hupu.jrs.otto.ChangeThemeEvent
import com.hupu.jrs.ui.BaseActivity
import com.hupu.jrs.util.SettingPrefUtil
import com.hupu.jrs.util.ThemeUtil
import com.squareup.otto.Bus
import java.util.HashMap
import javax.inject.Inject

/**
 * Created by sll on 2016/5/17.
 */
class ColorsDialogFragment : DialogFragment(), AdapterView.OnItemClickListener {

    @Inject
    internal var mBus: Bus? = null

    private val colorMap = HashMap<String, ColorDrawable>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SettingComponent::class.java.cast((activity as HasComponent<SettingComponent>).component)
                .inject(this)
    }

    override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
        isCancelable = true
        val view = View.inflate(activity, R.layout.dialog_md_colors, null)
        val gridView = view.findViewById<View>(R.id.grid) as GridView
        gridView.adapter = MDColorsAdapter()
        gridView.onItemClickListener = this
        return AlertDialogWrapper.Builder(activity).setView(view)
                .setPositiveButton("取消", null)
                .create()
    }

    internal inner class MDColorsAdapter : BaseAdapter() {

        override fun getCount(): Int {
            return ThemeUtil.themeColorArr.size
        }

        override fun getItem(position: Int): Any {
            return ThemeUtil.themeColorArr[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            var convertView = convertView
            if (convertView == null) {
                convertView = View.inflate(activity, R.layout.item_md_colors, null)
            }

            if (!colorMap.containsKey(position.toString())) {
                colorMap.put(position.toString(),
                        ColorDrawable(resources.getColor(ThemeUtil.themeColorArr[position][0])))
            }

            val imgColor = convertView!!.findViewById<View>(R.id.ivColor) as ImageView
            val colorDrawable = colorMap[position.toString()]
            imgColor.setImageDrawable(colorDrawable)

            val imgSelected = convertView.findViewById<View>(R.id.ivSelected)
            imgSelected.setVisibility(
                    if (SettingPrefUtil.getThemeIndex(activity) === position) View.VISIBLE else View.GONE)

            return convertView
        }
    }

    override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        if (position == SettingPrefUtil.getThemeIndex(activity)) {
            dismiss()
            return
        }
        SettingPrefUtil.setThemeIndex(activity, position)
        dismiss()
        mBus!!.post(ChangeThemeEvent())
        if (activity is BaseActivity) (activity as BaseActivity).reload()
    }

    companion object {

        fun launch(activity: Activity) {
            val fragment = activity.fragmentManager.findFragmentByTag("DialogFragment")
            if (fragment != null) {
                activity.fragmentManager.beginTransaction().remove(fragment).commit()
            }
            val dialogFragment = ColorsDialogFragment()
            dialogFragment.show(activity.fragmentManager, "DialogFragment")
        }
    }
}
